package com.questions.helper.util;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.OpenableColumns;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.fragment.app.FragmentActivity;

import java.io.FileNotFoundException;

public class FilePickerUtil {
    private ActivityResultLauncher<String> filePickerLauncher;
    private final FileSelectionListener listener;

    public FilePickerUtil(FragmentActivity activity, FileSelectionListener listener) {
        this.listener = listener;
        initializeFilePicker(activity);
    }

    private void initializeFilePicker(FragmentActivity activity) {
        filePickerLauncher = activity.registerForActivityResult(
                new ActivityResultContracts.GetContent(),
                uri -> {
                    if (uri != null) {
                        FileInfo fileInfo = getFileInfo(activity, uri);
                        if (listener != null) {
                            try {
                                listener.onFileSelected(fileInfo);
                            } catch (FileNotFoundException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    } else {
                        if (listener != null) {
                            listener.onSelectionCancelled();
                        }
                    }
                }
        );
    }

    public void openFilePicker(String mimeType) {
        filePickerLauncher.launch(mimeType);
    }

    public void openFilePicker() {
        openFilePicker("*/*");
    }

    private FileInfo getFileInfo(Context context, Uri uri) {
        FileInfo fileInfo = new FileInfo();
        fileInfo.setUri(uri);

        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);

                if (nameIndex != -1) {
                    fileInfo.setFileName(cursor.getString(nameIndex));
                }
                if (sizeIndex != -1) {
                    fileInfo.setFileSize(cursor.getLong(sizeIndex));
                }
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        // 获取MIME类型
        String mimeType = context.getContentResolver().getType(uri);
        fileInfo.setMimeType(mimeType);

        return fileInfo;
    }

    public static class FileInfo {
        private Uri uri;
        private String fileName;
        private long fileSize;
        private String mimeType;

        // Getter 和 Setter 方法
        public Uri getUri() { return uri; }
        public void setUri(Uri uri) { this.uri = uri; }

        public String getFileName() { return fileName; }
        public void setFileName(String fileName) { this.fileName = fileName; }

        public long getFileSize() { return fileSize; }
        public void setFileSize(long fileSize) { this.fileSize = fileSize; }

        public String getMimeType() { return mimeType; }
        public void setMimeType(String mimeType) { this.mimeType = mimeType; }
    }

    public interface FileSelectionListener {
        void onFileSelected(FileInfo fileInfo) throws FileNotFoundException;
        void onSelectionCancelled();
    }
}
